import { componentInfoType } from '../store/componentsReducer'
import { PageInfoPropsType } from '../store/pageInfoReducer'
import { QuestionType, ApiResponse } from '../types/QuestionType'
import http from '../utils/http'

/**
 * 问卷搜索参数
 * @param title 问卷标题
 * @param isDelete 是否删除
 * @param star 是否标星
 */
type QuestionSearchProp = {
  title: string
  isDelete: number
  star: boolean
  pageNum: number
  pageSize: number
}

// 获取问卷列表
export const getQuestionListFun = (
  params: Partial<QuestionSearchProp>
): Promise<ApiResponse<{ list: QuestionType[]; total: number }>> => {
  return http({
    url: '/question/list',
    method: 'get',
    params: params
  })
}

// 创建问卷
export type AddQuestionType = PageInfoPropsType & {
  componentsList: componentInfoType[]
}
export const addQuestionFun = (
  data: AddQuestionType
): Promise<ApiResponse<{ insertedId: string }>> => {
  return http({
    url: '/question/add',
    method: 'post',
    data
  })
}

// 查询问卷
export const getQuestionInfoFun = (
  _id: string
): Promise<ApiResponse<QuestionType>> => {
  return http({
    url: '/question/getInfo',
    method: 'get',
    params: {
      _id
    }
  })
}

// 根据ID更新数据
export const updateQuestionInfoFun = (
  data: Partial<QuestionType>
): Promise<ApiResponse<QuestionType>> => {
  return http({
    url: '/question/update',
    method: 'post',
    data
  })
}

// 根据ID假删除
export const deleteQuestionFun = (
  _id: string
): Promise<ApiResponse<{ _id: string }>> => {
  return http({
    url: '/question/delete',
    method: 'delete',
    params: {
      _id
    }
  })
}

// 根据id批量还原
export const restoreQuestionFun = (
  ids: string[]
): Promise<ApiResponse<null>> => {
  return http({
    url: '/question/restore',
    method: 'put',
    data: {
      ids
    }
  })
}

// 根据id批量真删除
export const removeQuestionFun = (
  ids: string[]
): Promise<ApiResponse<null>> => {
  return http({
    url: '/question/remove',
    method: 'put',
    data: {
      ids
    }
  })
}

// 根据问卷id获取答卷列表
type answerType = {
  _id: string
  value: string
}
export type answerParamType = {
  questionId: string
  pageNum: number
  pageSize: number
}
export const getAnswerListFun = (
  params: answerParamType
): Promise<
  ApiResponse<{
    total: number
    rows: answerType[]
  }>
> => {
  return http({
    url: '/question/getAnswerList',
    method: 'get',
    params
  })
}

// 根据组件id获取统计数据
export type statParamType = {
  questionId: string
  componentId: string
  name: string
}
export type statDataType = Array<{
  value: string
  count: number
}>
export const getStatDataFun = (
  params: statParamType
): Promise<ApiResponse<statDataType>> => {
  return http({
    url: '/question/stat',
    method: 'get',
    params
  })
}

// 获取问卷统计数量
export type QuestionStatisticsType = {
  questionTotal: number
  published: number
  answered: number
}
export const getQuestionStatisticsFun = (): Promise<
  ApiResponse<QuestionStatisticsType>
> => {
  return http({
    url: '/question/getQuestionStatistics',
    method: 'get'
  })
}
